//
//  DownloadSoundQualitVC.swift
//  UNHimalayas
//
//  Created by 王水平 on 2019/11/27.
//  Copyright © 2019 HachiTech. All rights reserved.
//

import UIKit

class DownloadSoundQualitVC: UIViewController,UITableViewDelegate,UITableViewDataSource {

    var tableView : UITableView!
    var dataArray = [[String:String]()]
    
    var selectIndex = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.edgesForExtendedLayout = []
        self.navigationItem.title = "下载音质"
        self.view.backgroundColor = RGBA(240, g: 240, b: 240, a: 1.0);
        
        initAction()
        initTabelView()
        initFooterView()
        
        dataArray = [
                ["title":"智能选择","content":"WIFI下声音品质更高，移动数据收听时更省流量"],
                ["title":"标准音质","content":"每条声音约1-5M，较少流量拥有收听好品质"],
                ["title":"高清音质","content":"每条声音约2-10M，更高收听品质的选择"],
          ]
        tableView.reloadData()
    }
    
    func initAction() -> Void {
        
        let leftBtn = UIButton()
        leftBtn.setImage(UIImage(named: "navi_back"), for: UIControl.State.normal)
        leftBtn.setImage(UIImage(named: "navi_back"), for: UIControl.State.highlighted)
           
        leftBtn.addTarget(self, action: #selector(DownloadSoundQualitVC.leftAction), for: UIControl.Event.touchUpInside)
        // button自适应大小
        leftBtn.sizeToFit()
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView:leftBtn)
    }
    
    func initTabelView() -> Void {
        
        self.tableView = UITableView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 0), style: .grouped)
        self.tableView.backgroundColor = UIColor.clear
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kRealWidth(10)))
        self.view.addSubview(self.tableView)
        
        self.tableView.snp.makeConstraints { (make) in
            
            make.edges.equalTo(self.view)
        }
    }
    
    func initFooterView() -> Void {
        
        let label = UILabel(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kRealWidth(20)))
        label.backgroundColor = UIColor.clear
        label.text = "若没有高清音质会自动下载标清音质"
        label.textColor = UIColor.orange
        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: 14)
        self.tableView.tableFooterView = label
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cellName = "DownloadSoundQualitViewCellName"
        let cell = DownloadSoundQualitViewCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: cellName)
        
        let dict = dataArray[indexPath.row]
        
        cell.titleLabel.text = dict["title"]
        cell.contentLabel.text = dict["content"]
        
        cell.selectImgView.isHidden = selectIndex != indexPath.row
        
        return cell;
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return kRealWidth(70)
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        selectIndex = indexPath.row
        
        tableView.reloadData()
    }
    
    // MARK: - 点击
    
    @objc private func leftAction() -> Void {
        
        self.navigationController?.popViewController(animated: true)
    }

}
